package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.response.AlipayBindStatusResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.widget.TimeCount;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;
import retrofit2.Retrofit;

public class BindZfbActivity extends BaseActivity {

    public static void start(Context context, String zfb) {
        Intent intent = new Intent(context, BindZfbActivity.class);
        intent.putExtra("zfb", zfb);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_zfb)
    EditText etZfb;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    TextView btnCode;

    private String mobile = "", uid = "", code = "";
    private TimeCount timeCount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_zfb;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("账号绑定");

        String zfb=getIntent().getStringExtra("zfb");
        if("支付宝".equals(zfb)){
            etZfb.setText("");
        }else{
            etZfb.setText(getIntent().getStringExtra("zfb"));
        }
        uid = SharePrefUtils.getUserId();
        timeCount = new TimeCount(60000, 1000, btnCode);
    }

    @Override
    protected void setListener() {
    }

    @OnClick({R.id.title_finish, R.id.btn_code, R.id.btn_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.btn_code:
//                mobile = etZfb.getText().toString().trim();
//                if (TextUtils.isEmpty(mobile)) {
//                    ToastUtils.showToast("请输入手机号");
//                    return;
//                }
//                postCode();

                getCode();
                break;
            case R.id.btn_sure:

                mobile = etZfb.getText().toString().trim();
                code = etCode.getText().toString().trim();
                String name = etName.getText().toString().trim();

                if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(code) || TextUtils.isEmpty(name)) {
                    ToastUtils.showToast("信息不能为空");
                    return;
                }

                mApiHelper.bindAli(uid, name, mobile, code, new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        ToastUtils.showToast(baseBean.getMs());
                        if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

                break;
        }
    }

    private void postCode() {
        timeCount.start();
        mApiHelper.getMsmCode(mobile, new Observer<BaseBean<String>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseBean<String> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    code = baseBean.getData();
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        ViewUtils.closeKeyboard(this);
    }


    /**
     * 获取我的账户
     */
    private void getCode() {
        timeCount.start();
        OkHttpUtils.post()
                .url(UrlHost.SEND_CODE)
                .addParams("uid", SharePrefUtils.getUserId())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    AlipayBindStatusResponse alipayBindStatusResponse = JsonUtils.parseByGson(response, AlipayBindStatusResponse.class);
                    if (alipayBindStatusResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(alipayBindStatusResponse.getCode())) {
                        ToastUtils.showToast(alipayBindStatusResponse.getMs());
                    } else {
                        ToastUtils.showToast(alipayBindStatusResponse.getMs());
                    }
                } else {
                }
            }
        });
    }

    /**
     * 获取我的账户
     */
    private void bindAliPay() {
        timeCount.start();
        OkHttpUtils.post()
                .url(UrlHost.BING_ALIPAY)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("alicode", etZfb.getText().toString())
                .addParams("code",etCode.getText().toString())
                .addParams("name", etName.getText().toString())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    AlipayBindStatusResponse alipayBindStatusResponse = JsonUtils.parseByGson(response, AlipayBindStatusResponse.class);
                    if (alipayBindStatusResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(alipayBindStatusResponse.getCode())) {
                        ToastUtils.showToast(alipayBindStatusResponse.getMs());
                    } else {
                        ToastUtils.showToast(alipayBindStatusResponse.getMs());
                    }
                } else {
                }
            }
        });
    }
}
